package com.zhai.boot.util;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;

/**
 * @author Created by 翟凤玺
 * @package com.zhai.boot.util
 * @project-name zhai-boot
 * @description 日期业务 工具类
 * @create-datetime 2019-9-18 13:52
 */
public class DateBizUtil {

    /**
     * 创建时间类型
     */
    private static final String CREATE_TIME_TYPE_TODAY = "TODAY";
    private static final String CREATE_TIME_TYPE_YESTERDAY = "YESTERDAY";
    private static final String CREATE_TIME_TYPE_WEEK = "WEEK";
    private static final String CREATE_TIME_TYPE_LASTWEEK = "LASTWEEK";
    private static final String CREATE_TIME_TYPE_MONTH = "MONTH";
    private static final String CREATE_TIME_TYPE_LASTMONTH = "LASTMONTH";

    @Data
    public static class ExportDate {

        @ApiModelProperty(value = "开始时间")
        private String startDate;

        @ApiModelProperty(value = "结束时间")
        private String endDate;
    }

    public static ExportDate getDate(String createTimeType) {
        LocalDateTime now = DateUtil.getCurrentLocalDateTime();
        String start = null;
        String end = null;
        ExportDate exportDate = new ExportDate();
        switch (createTimeType){
            case CREATE_TIME_TYPE_TODAY:
                start = DateUtil.toTimestampStr(DateUtil.getZerolizedTime(now));
                end = DateUtil.toTimestampStr(DateUtil.getEndTime(now));
                break;
            case CREATE_TIME_TYPE_YESTERDAY:
                start = DateUtil.toTimestampStr(DateUtil.getZerolizedTime(now.minusDays(1)));
                end = DateUtil.toTimestampStr(DateUtil.getEndTime(now.minusDays(1)));
                break;
            case CREATE_TIME_TYPE_WEEK:
                start = DateUtil.toTimestampStr(DateUtil.getZerolizedTime(now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY))));
                end = DateUtil.toTimestampStr(DateUtil.getEndTime(now.with(TemporalAdjusters.next(DayOfWeek.SUNDAY))));
                break;
            case CREATE_TIME_TYPE_LASTWEEK:
                start = DateUtil.toTimestampStr(DateUtil.getZerolizedTime((now.minusDays(7).with(TemporalAdjusters.previous(DayOfWeek.MONDAY)))));
                end = DateUtil.toTimestampStr(DateUtil.getEndTime(now.with(TemporalAdjusters.previous(DayOfWeek.SUNDAY))));
                break;
            case CREATE_TIME_TYPE_MONTH:
                start = DateUtil.toTimestampStr(DateUtil.getZerolizedTime(now.with(TemporalAdjusters.firstDayOfMonth())));
                end = DateUtil.toTimestampStr(DateUtil.getEndTime(now.with(TemporalAdjusters.lastDayOfMonth())));
                break;
            case CREATE_TIME_TYPE_LASTMONTH:
                start = DateUtil.toTimestampStr(DateUtil.getZerolizedTime(now.minusMonths(1).with(TemporalAdjusters.firstDayOfMonth())));
                end = DateUtil.toTimestampStr(DateUtil.getEndTime(now.minusMonths(1).with(TemporalAdjusters.lastDayOfMonth())));
                break;
            default: break;
        }
        exportDate.setStartDate(start);
        exportDate.setEndDate(end);
        return exportDate;
    }

    public static void main(String[] args) {
        ExportDate today = DateBizUtil.getDate(DateBizUtil.CREATE_TIME_TYPE_TODAY);
        System.out.println("今天：" + DateUtil.timestampStrToDateStr(today.getStartDate(), DateUtil.DATETIME_FMT_3) + "," + DateUtil.timestampStrToDateStr(today.getEndDate(), DateUtil.DATETIME_FMT_3));
        ExportDate yesterday = DateBizUtil.getDate(DateBizUtil.CREATE_TIME_TYPE_YESTERDAY);
        System.out.println("昨天：" + DateUtil.timestampStrToDateStr(yesterday.getStartDate(), DateUtil.DATETIME_FMT_3) + "," + DateUtil.timestampStrToDateStr(yesterday.getEndDate(), DateUtil.DATETIME_FMT_3));
        ExportDate week = DateBizUtil.getDate(DateBizUtil.CREATE_TIME_TYPE_WEEK);
        System.out.println("本周：" + DateUtil.timestampStrToDateStr(week.getStartDate(), DateUtil.DATETIME_FMT_3) + "," + DateUtil.timestampStrToDateStr(week.getEndDate(), DateUtil.DATETIME_FMT_3));
        ExportDate lastweek = DateBizUtil.getDate(DateBizUtil.CREATE_TIME_TYPE_LASTWEEK);
        System.out.println("上周：" + DateUtil.timestampStrToDateStr(lastweek.getStartDate(), DateUtil.DATETIME_FMT_3) + "," + DateUtil.timestampStrToDateStr(lastweek.getEndDate(), DateUtil.DATETIME_FMT_3));
        ExportDate month = DateBizUtil.getDate(DateBizUtil.CREATE_TIME_TYPE_MONTH);
        System.out.println("本月：" + DateUtil.timestampStrToDateStr(month.getStartDate(), DateUtil.DATETIME_FMT_3) + "," + DateUtil.timestampStrToDateStr(month.getEndDate(), DateUtil.DATETIME_FMT_3));
        ExportDate lastmonth = DateBizUtil.getDate(DateBizUtil.CREATE_TIME_TYPE_LASTMONTH);
        System.out.println("上月：" + DateUtil.timestampStrToDateStr(lastmonth.getStartDate(), DateUtil.DATETIME_FMT_3) + "," + DateUtil.timestampStrToDateStr(lastmonth.getEndDate(), DateUtil.DATETIME_FMT_3));
    }

}